package com.qianzhou.eduCourseBoot.service.impl;

import com.qianzhou.eduCommon.bo.CourseSectionBO;
import com.qianzhou.eduCommon.dto.CourseLessonDTO;
import com.qianzhou.eduCommon.dto.CourseSectionDTO;
import com.qianzhou.eduCommon.mapper.CourseLessonMapper;
import com.qianzhou.eduCommon.mapper.CourseMapper;
import com.qianzhou.eduCommon.mapper.CourseSectionMapper;
import com.qianzhou.eduCommon.po.Course;
import com.qianzhou.eduCommon.po.CourseLesson;
import com.qianzhou.eduCommon.po.CourseSection;
import com.qianzhou.eduCommon.vo.CourseLessonVO;
import com.qianzhou.eduCommon.vo.CourseSectionVO;
import com.qianzhou.eduCommon.vo.CourseVO;
import com.qianzhou.eduCourseBoot.converter.CourseConverter;
import com.qianzhou.eduCourseBoot.converter.CourseLessonConverter;
import com.qianzhou.eduCourseBoot.converter.CourseSectionConverter;
import com.qianzhou.eduCourseBoot.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseContentServiceImpl implements CourseContentService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseSectionMapper courseSectionMapper;
    @Autowired
    private CourseLessonMapper courseLessonMapper;
    /*/**
     * @Author: qian.zhou
     * @Description: 根据课程的id查找对应的章节和课时信息
     * @DateTime: 2021/10/12 10:46
     * @Params: * @Param courseId:
     * @Return: * @return: java.util.List<com.qianzhou.eduCommon.vo.CourseSectionVO>
     */
    @Override
    public List<CourseSectionVO> findSectionAndLesson(Integer courseId) {
        List<CourseSectionBO> courseSectionBOS = courseMapper.findSectionAndLesson(courseId);
        /**
         * 将查询后的BOS转换为VOS
         */
        List<CourseSectionVO> courseSectionVOS = new ArrayList<>(courseSectionBOS.size());
        /**
         * 使用Converter转换器
         */
        courseSectionVOS = CourseSectionConverter.INSTANCE.boListConvertVoList(courseSectionBOS);
        /**
         * for 循环 实现嵌套列表converter
         */
        int num = 0;
        for (CourseSectionBO courseSectionBo:courseSectionBOS
             ) {
            if (courseSectionBo.getCourseLessons()!=null){
                List<CourseLessonVO> courseLessonVOS = CourseSectionConverter.INSTANCE.poListConvertVoList(courseSectionBo.getCourseLessons());
                courseSectionVOS.get(num).setCourseLessonVOS(courseLessonVOS);
            }
            num++;
        }
        return courseSectionVOS;
    }

    @Override
    public Integer updateSectionStatus(CourseSectionDTO dto) {
        /**
         * Converter转换器将dto转换为po
         */
        CourseSection courseSection = CourseSectionConverter.INSTANCE.dtoConvertPo(dto);
        return courseSectionMapper.updateById(courseSection);
    }

    @Override
    public CourseVO findCourseById(Integer courseId) {
        Course course = courseMapper.selectById(courseId);
        /**
         * convert转换器将course转换为courseVo
         */
        return CourseConverter.INSTANCE.poConvertVo(course);
    }

    @Override
    public CourseSectionVO showCourseSection(Integer courseSectionId) {
        CourseSection courseSection = courseSectionMapper.selectById(courseSectionId);
        return  CourseSectionConverter.INSTANCE.poConvertVoSection(courseSection);
    }

    @Override
    public CourseLessonVO showCourseLesson(Integer courseLessonId) {
        CourseLesson courseLesson = courseLessonMapper.selectById(courseLessonId);
        return CourseLessonConverter.INSTANCE.poConvertVo(courseLesson);
    }

    @Override
    public void updateSection(CourseSectionDTO dto) {
        CourseSection courseSection = CourseSectionConverter.INSTANCE.dtoConvertPo(dto);
        courseSectionMapper.updateById(courseSection);
    }

    @Override
    public void saveSection(CourseSectionDTO dto) {
        CourseSection courseSection = CourseSectionConverter.INSTANCE.dtoConvertPo(dto);
        courseSectionMapper.insert(courseSection);
    }

    @Override
    public void updateLesson(CourseLessonDTO dto) {
        CourseLesson courseLesson = CourseLessonConverter.INSTANCE.dtoConvertPo(dto);
        courseLessonMapper.updateById(courseLesson);
    }

    @Override
    public void saveLesson(CourseLessonDTO dto) {
        CourseLesson courseLesson = CourseLessonConverter.INSTANCE.dtoConvertPo(dto);
        courseLessonMapper.insert(courseLesson);
    }

    @Override
    public void updateLessonStatus(CourseLessonDTO dto) {
        CourseLesson courseLesson = CourseLessonConverter.INSTANCE.dtoConvertPo(dto);
        courseLessonMapper.updateById(courseLesson);
    }
}
